package com.qingxin.wuye.action;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.wuye.entity.WyRepairOrders;
import com.qingxin.wuye.service.WyRepairOrdersService;

@Controller
@RequestMapping("/repairOrders")
public class RepairOrdersAction extends BaseAction {

	@Autowired
	private WyRepairOrdersService repairOrdersService;
	
	
	/**
	 * 列表页
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "wuye/repairOrders/repairOrders_list";
	}
	
	
	/**
	 * 获取列表分页数据
	 * @param datatype
	 * @param leftdata
	 * @param roomCode
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String datatype, String leftdata, String roomCode, PageParam pageParam, HttpServletRequest request){
		String companyid = null;
		String gardenid = null;
		if(SystemCfg.UI_DATATYPE_COMPANY.equalsIgnoreCase(datatype)){
			companyid = leftdata;
		}else if(SystemCfg.UI_DATATYPE_GARDEN.equalsIgnoreCase(datatype)){
			gardenid = leftdata;
		}
		LoginUser loginUser = getLoginUser(request);
		if(!SystemCfg.isAdminUser(loginUser)){
			companyid = loginUser.getCompanyid();
		}
		
		return repairOrdersService.list(companyid, gardenid, roomCode, pageParam, getLoginUser(request));
	}
	
	
	/**
	 * 添加信息
	 * @param model
	 * @param buildingid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, @RequestParam(value="leftdata")String gardenid, HttpServletRequest request){
		model.put("gardenid", gardenid);
		return "wuye/repairOrders/repairOrders_add";
	}
	
	
	/**
	 * 保存
	 * @param orders
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid WyRepairOrders orders, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = repairOrdersService.add(orders, getLoginUser(request), request);
		return info;
	}

	
//	/**
//	 * 修改
//	 * @param model
//	 * @param ordersid
//	 * @return
//	 */
//	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
//	public String edit(ModelMap model, String ordersid){
//		WyRepairOrders orders = repairOrdersService.get(ordersid);
//		model.put("orders", orders);
//		
//		return "wuye/repairOrders/repairOrders_edit";
//	}
//	
//	
//	/**
//	 * 保存修改
//	 * @param orders
//	 * @param result
//	 * @param request
//	 * @return
//	 */
//	@RequestMapping(value="/update.json", method=RequestMethod.POST)
//	@ResponseBody
//	public Object update(@Valid WyRepairOrders orders, BindingResult result, HttpServletRequest request){
//		if(result.hasErrors()){
//			return getSysErrors(result);
//		}
//		
//		SysError info = repairOrdersService.up
//		return info;
//	}
	
	
	/**
	 * 删除
	 * @param ordersid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] ordersid, HttpServletRequest request){
		if(ordersid==null || ordersid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return repairOrdersService.delete(ordersid, getLoginUser(request), request);
	}
	
	
}
